package com.fare.taluo.application.impl.crawl.crawlstatistics;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayatang.domain.InstanceFactory;
import com.fare.taluo.application.crawl.crawlstatistics.CrawlStatisticsTaskApplicaion;
import com.fare.taluo.application.crawl.crawlstatistics.FdcCrawlStatisticsApplication;
import com.fare.taluo.application.crawl.crawlstatistics.FdcCrawlStatisticsForFlightLineApplication;
import com.fare.taluo.application.crawl.crawlstatistics.FdcCrawlStatisticsForGrabDateApplication;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.core.domain.crawl.statistics.StatisticsTotalEntity;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.vo.core.SourceEntityVO;
import com.fare.taluo.vo.core.StatisticsTotalDetailEntityVO;
import com.fare.taluo.vo.core.StatisticsTotalEntityVO;
import com.fare.taluo.vo.crawl.statistics.CrawlStatisticsVO;
import com.fare.taluo.vo.crawl.statistics.FdcCrawlStatisticsForFlightLineVo;
import com.fare.taluo.vo.crawl.statistics.FdcCrawlStatisticsForGrabDateVo;
import com.fare.taluo.vo.crawl.statistics.FdcCrawlStatisticsVO;
@Transactional
@Scope("singleton")
@Service("crawlStatisticsTaskApplicaion")
public class CrawlStatisticsTaskApplicaionImpl implements CrawlStatisticsTaskApplicaion{
	
	/**
	 * 持久化后处理
	*/
	//按抓取日期
	@Override
	public List<CrawlStatisticsVO> showByGrabDate(String date1, String date2) {
		List<CrawlStatisticsVO> listVo=new ArrayList<CrawlStatisticsVO>();
		List<FdcCrawlStatisticsForGrabDateVo> listFdcVo=InstanceFactory.getInstance(FdcCrawlStatisticsForGrabDateApplication.class).findByGrabDate(date1, date2);
		List<SourceEntityVO> listSourceVo =InstanceFactory.getInstance(SourceApplication.class).findAllSourceEntity();
		Map<String,SourceEntityVO >sourceMap=new HashMap<String, SourceEntityVO>();
		if(listSourceVo.size()>0){
			for(int k=0;k<listSourceVo.size();k++){
				SourceEntityVO vo=new SourceEntityVO();
				vo=listSourceVo.get(k);
				String key=vo.getHbaseKey();
				if(sourceMap.get(key)==null){
					sourceMap.put(key, vo);
				}
			}
		}
		
		if(listFdcVo.size()>0){
			for(FdcCrawlStatisticsForGrabDateVo CSvo:listFdcVo){
				CrawlStatisticsVO vo=new CrawlStatisticsVO();
				vo.setAllLines(CSvo.getDayLineNumbers().intValue());
				vo.setAllSourceSizeLong(CSvo.getDaySourceDatas());
				vo.setAllSourceSize(CSvo.getDaySourceDatas().toString());
				
				String sourceByOneChannel=""; 
				DecimalFormat r=new DecimalFormat(); 
				r.applyPattern("#0.00");//保留小数位数，不足会补零 
				Map<String ,String > mapSourceStr=doStrings(CSvo.getSourceByChannel());
				for(Map.Entry<String, String> mapStr:mapSourceStr.entrySet()){
					String name=sourceMap.get(mapStr.getKey()).getName();
					sourceByOneChannel+=(r.format(Long.parseLong(mapStr.getValue())/(1024*1024)))+"M @ "+name+"</br>";
				}
				vo.setSourceSizeStr(sourceByOneChannel);
				
				String lineByChannelStr="";
				Map<String ,String > mapLineStr=doStrings(CSvo.getLineByChannel());
				for(Map.Entry<String, String> mapStr:mapLineStr.entrySet()){
					String name=sourceMap.get(mapStr.getKey()).getName();
					lineByChannelStr+=mapStr.getValue()+"条航线 @ "+name	+"</br>";
				}
				vo.setGraResourceStr(lineByChannelStr);
				
				vo.setId(CSvo.getId());
				vo.setDateStr(CSvo.getGrabDateStr());
				
				listVo.add(vo);
			}
		}
		
		if(listVo.size()>0){//进行日期排序后输出
			sortClass sort=new sortClass();
			Collections.sort(listVo,sort);
		}
		
		return listVo;
	}
	
	//按航线
	@Override
	public List<StatisticsTotalEntityVO> getDataByLine(String fromCityCode, String toCityCode) {
		List<StatisticsTotalEntityVO> listVo=new ArrayList<StatisticsTotalEntityVO>();
		List<FdcCrawlStatisticsForFlightLineVo> FDCListVo =InstanceFactory.getInstance(FdcCrawlStatisticsForFlightLineApplication.class).findForFlightLineVo(fromCityCode, toCityCode);
		if(FDCListVo.size()>0){
			for(FdcCrawlStatisticsForFlightLineVo fdcVo:FDCListVo){
				StatisticsTotalEntityVO vo=new StatisticsTotalEntityVO();
				vo.setId(fdcVo.getId()+"");
				vo.setFromCity(fdcVo.getFromCity());
				vo.setToCity(fdcVo.getToCity());
				vo.setFromCityName(fdcVo.getFromCityName());
				vo.setToCityName(fdcVo.getToCityName());
				vo.setGrabStartDate(fdcVo.getGrabStartDateStr());
				vo.setGrabEndDate(fdcVo.getGrabEndDateStr());
				vo.setSourceDataSize(fdcVo.getGrabTotalSource());
				vo.setSourceDataSizeStr(fdcVo.getGrabTotalSource()+"");
				
				listVo.add(vo);
			}
		}
		
		return listVo;
	}
	
	
	//历史数据
	@Override
	public List<StatisticsTotalEntityVO> getHistoryDatas(String fromCity,String toCity, String grabDate1, String grabDate2) {
		List<FdcCrawlStatisticsVO> FDCList=InstanceFactory.getInstance(FdcCrawlStatisticsApplication.class).findByGrabDate(fromCity, toCity, grabDate1, grabDate2);
		List<StatisticsTotalEntityVO> listVo=new ArrayList<StatisticsTotalEntityVO>();
		if(FDCList.size()>0){
			for(FdcCrawlStatisticsVO fdcVo:FDCList){
				StatisticsTotalEntityVO vo=new StatisticsTotalEntityVO();
				vo.setFromCity(fdcVo.getFromCity());
				vo.setToCity(fdcVo.getToCity());
				vo.setFromCityName(fdcVo.getFromCityName());
				vo.setToCityName(fdcVo.getToCityName());
				vo.setSourceDataSize(fdcVo.getDataSources());
				vo.setSourceDataSizeStr(fdcVo.getDataSources()+"");
				vo.setId(fdcVo.getId()+"");
				vo.setGrabdate(fdcVo.getGrabDateStr());
				
				listVo.add(vo);
			}
		}
		
		return listVo;
	}
	
	
	//单独处理页面数据
	@SuppressWarnings("unchecked")
	public List<CrawlStatisticsVO> showByDate(List<StatisticsTotalEntityVO> voList){
		List<CrawlStatisticsVO> dateStrList=new ArrayList<CrawlStatisticsVO>();
		Map<String,List<StatisticsTotalEntityVO>> maps=new LinkedHashMap<String,List<StatisticsTotalEntityVO>>();
		List<StatisticsTotalEntityVO> listVO=null;
		List<StatisticsTotalEntityVO> newlLstVO=null;
		Map<String,List<StatisticsTotalEntityVO>> mapLines=new LinkedHashMap<String,List<StatisticsTotalEntityVO>>();
		Map<String,List<StatisticsTotalEntityVO>> mapLinesForDate=new LinkedHashMap<String,List<StatisticsTotalEntityVO>>();
		Map<String ,List<String>> differentCityLines=new LinkedHashMap<String, List<String>>();
		for(int i=0;i<voList.size();i++){
			StatisticsTotalEntityVO old=voList.get(i);
			String key=old.getGrabdate();//过滤抓取日期
			String nowadayStr=DateUtils.getDateStr("YYYY-MM-dd");
			if(!nowadayStr.equals(old.getGrabdate())){//过滤掉今天的抓取
				if(maps.get( key )!=null){
					int k=1;
					k++;
						
					newlLstVO=maps.get(key);//将存在的数据取出来
					newlLstVO.add(old);
					maps.put(key,newlLstVO);
				}else{
					listVO=new ArrayList<StatisticsTotalEntityVO>();
					listVO.add(old);
					maps.put(key,listVO);
				}
				
			}
		}
		
		
		//单独过滤出某抓取日期下不重复航线
		List<StatisticsTotalEntityVO> listLineVO=null;
		List<StatisticsTotalEntityVO> newLineVO=null;
		//过滤掉今天的抓取
		for(int line=0;line<voList.size();line++){
			StatisticsTotalEntityVO olds=voList.get(line);
			String keyLines=olds.getGrabdate()+"--"+olds.getFromCity()+"--"+olds.getToCity();//拼装成一个line_key
			String nowadayStr=DateUtils.getDateStr("YYYY-MM-dd");
			if(!nowadayStr.equals(olds.getGrabdate())){
				if(mapLines.get(keyLines) !=null){
					newLineVO=mapLines.get(keyLines);
					newLineVO.add(olds);
					mapLines.put(keyLines, newLineVO);
				}else{
					listLineVO=new ArrayList<StatisticsTotalEntityVO>();
					listLineVO.add(olds);
					mapLines.put(keyLines, listLineVO);
				}
			}
		}
		if(mapLines.size()>0){
			
			for(Map.Entry<String,List<StatisticsTotalEntityVO>> dataLines:mapLines.entrySet()){
				List<String > cityLegLists=new ArrayList<String>();
				String keys=dataLines.getKey();
				String grabDateStr=keys.split("\\--")[0];
				String cityLegs=keys.split("\\--")[1]+"-"+keys.split("\\--")[2];
				if(differentCityLines.get(grabDateStr)!=null){
					cityLegLists.add(cityLegs);
					cityLegLists.addAll(differentCityLines.get(grabDateStr));
				}else{
					cityLegLists.clear();
					cityLegLists.add(cityLegs);
				}
				differentCityLines.put(grabDateStr, cityLegLists);
			}
		}
		
		Object s[] = maps.keySet().toArray();
		for(int i = 0; i < maps.size(); i++) {
			CrawlStatisticsVO showVo=new CrawlStatisticsVO();
			List<StatisticsTotalEntityVO> stu=maps.get(s[i]);
			List<StatisticsTotalEntityVO> listForGrabVO=null;
			List<StatisticsTotalEntityVO> newlLstForGrabVO=null;
			Map<String,List<StatisticsTotalEntityVO>> map=new LinkedHashMap<String,List<StatisticsTotalEntityVO>>();
			for(int j=0;j<stu.size();j++){
				StatisticsTotalEntityVO old=voList.get(j);
				String key=old.getGrabchannel();//过滤抓取渠道
				StatisticsTotalEntityVO olds=voList.get(j);
				if(map.get(key) !=null){
					newlLstForGrabVO=map.get(key);
					
					newlLstForGrabVO.add(olds);
					map.put(key, newlLstForGrabVO);
				}else{
					listForGrabVO=new ArrayList<StatisticsTotalEntityVO>();
					listForGrabVO.add(olds);
					map.put(key, listForGrabVO);
				}
			}
			
			List sourcelist=new ArrayList();
			Iterator iter = map.entrySet().iterator();  //获得map的Iterator
			while(iter.hasNext()) {
				Entry entry = (Entry)iter.next();
				sourcelist.add(entry.getValue());
			}
			Long allSizes=0L;
			StringBuffer resourceStr=new StringBuffer();
			StringBuffer sourceSizeStr=new StringBuffer();
			DecimalFormat r=new DecimalFormat(); 
			r.applyPattern("#0.00");//保留小数位数，不足会补零 
			if(map.size()>0){
				for(Map.Entry<String,List<StatisticsTotalEntityVO>> mapList:map.entrySet()){
					
					String resourceName=mapList.getKey();
					List<StatisticsTotalEntityVO>resourceList=mapList.getValue();
					//航线统计
					sourceSizeStr.append("<p>"+resourceList.size()+"条航线 @ "+resourceName+"&nbsp;</p>");
					
					Double resourceSizes=0.0;
					String theResourceList=null;
					for(int m=0;m<resourceList.size();m++){
						//流量统计
						StatisticsTotalEntityVO indexVo=resourceList.get(m);
						resourceSizes+=indexVo.getSourceDataSize();
						allSizes+=indexVo.getSourceDataSize();
					}
					theResourceList="<p>"+(r.format(resourceSizes/(1024*1024)))+"M @ "+resourceName+"&nbsp;</p>";
					resourceStr.append(theResourceList);
					
				}
			}
			String dateStr=(String) s[i];
		//	int allLines=stu.size();
			showVo.setAllLines(differentCityLines.get(dateStr).size());
			showVo.setDateStr(dateStr);
			showVo.setAllSourceSize(new Double(r.format(allSizes/(1024*1024))).longValue()+"M");
			showVo.setAllSourceSizeLong(new Double(r.format(allSizes/(1024*1024))).longValue());
			showVo.setGraResourceStr(sourceSizeStr.toString());
			showVo.setSourceSizeStr(resourceStr.toString());
			dateStrList.add(showVo);
		}
		
//		return voLists;
		return dateStrList;
	}
	
	
	
	//total
	private Long countadd(Long a,Long b){
		if(a!=null&&b!=null)
			return a+b;
		if(a==null & b==null)//这里用& 和  && 小心点
		    return 0l;
		if(a==null & b!=null)
			return b;
		if(b==null & a!=null)
			return a;
		return 0l;
	}
	//判断类型为空，赋值0
	private Long nullToLong(Long a,Long b){
		if(a!=null&&b!=null)
			return a+b;
		if(a==null & b==null)//这里用& 和  && 小心点
		    return 0l;
		if(a==null & b!=null)
			return b;
		if(b==null & a!=null)
			return a;
		return 0l;
	}
	
	//平均时间
	private Long avgTime(Long a,Long b){
		if(a!=null&&b!=null)
			return (a+b)/2;
		if(a==null & b==null)//这里用& 和  && 小心点
		    return 0l;
		if(a==null & b!=null)
			return b/2;
		if(b==null & a!=null)
			return a/2;
		return 0l;
	}
	
	//比较出两最大日期
	public String maxDate(String date1,String date2){
		SimpleDateFormat fmt = new SimpleDateFormat ("yyyy-MM-dd"); 
		String reStr=null;
		try {
			if (date1 != null && !date1.equals("") && date2 != null
					&& !date2.equals("")) {
				if (fmt.parse(date1).after(fmt.parse(date2))) {
					reStr = date1.split("\\ ")[0];
				}else{
					reStr = date2.split("\\ ")[0];
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return reStr;
	}
	
	
	@SuppressWarnings({ "unchecked", "unused" })
	private List<StatisticsTotalEntityVO> pingjieMethod(List<StatisticsTotalEntityVO> list) {
		DecimalFormat r=new DecimalFormat(); 
		r.applyPattern("#0.00");//保留小数位数，不足会补零 
		Map<String,StatisticsTotalEntityVO> map=new LinkedHashMap<String,StatisticsTotalEntityVO>();
		for (int i = 0; i < list.size(); i++) {
			StatisticsTotalEntityVO old=list.get(i);
			String nowadayStr=DateUtils.getDateStr("YYYY-MM-dd");
//			String key=old.getGrabchannel()+"--"+old.getGrabdate()+"--"+old.getFromCity()+"--"+old.getToCity();//拼装成一个key
			String key=old.getFromCity()+"--"+old.getToCity();//拼装成一个key
			if(!nowadayStr.equals(old.getGrabdate())){//过滤掉今天抓取的
				if(map.get( key )!=null){//判断已经存在同个日期，同个渠道，成功数相同，实际抓取数相同，同一出发城市，同一到到达城市 的数据
					int k=1;
					k++;
					StatisticsTotalEntityVO newVo=map.get(key);//将存在的数据取出来
					  //累加
					  Long total=nullToLong(old.getTotal(),newVo.getTotal());
					  Long failNum=nullToLong(old.getFailNum(),newVo.getFailNum());
					  Long secondFail=nullToLong(old.getSecondFail(),newVo.getSecondFail());
					  Long thirdFail=nullToLong(old.getThirdFail(),newVo.getThirdFail());
					  Long forthFail=nullToLong(old.getForthFail(),newVo.getForthFail());
					  Long otherFail=nullToLong(old.getOtherFail(),newVo.getOtherFail());
					  Long waiting=nullToLong(old.getWaiting(),newVo.getWaiting());
					  Long starting=nullToLong(old.getStarting(),newVo.getStarting());
					  Long working=nullToLong(old.getWorking(),newVo.getWorking());
					  Long actualNum=nullToLong(old.getActualNum(), newVo.getActualNum());
					  Long successNum=nullToLong(old.getSuccessNum(), newVo.getSuccessNum());
					  Long maxTime=nullToLong(old.getMaxTime(), newVo.getMaxTime());
					  Long minTime=nullToLong(old.getMinTime(), newVo.getMinTime());
					  Long allDataSizes=nullToLong(old.getSourceDataSize(),newVo.getSourceDataSize());
					  //newVo.setTotal(total-newVo.getSuccessNum());//total在数据库中多加了一遍成功数
					  String maxDate=maxDate(old.getStartTime(), newVo.getStartTime());
					  newVo.setStartTime(maxDate);
					  newVo.setTotal(total);
					  newVo.setFailNum(failNum);
					  newVo.setSecondFail(secondFail);
					  newVo.setThirdFail(thirdFail);
					  newVo.setForthFail(forthFail);
					  newVo.setOtherFail(otherFail);
					  newVo.setWaiting(waiting/k);
					  newVo.setStarting(starting);
					  newVo.setWorking(working);
					  newVo.setMaxTime(maxTime/k);
					  newVo.setMinTime(minTime/k);
					  newVo.setActualNum(actualNum/k);
					  newVo.setSuccessNum(successNum);
					  newVo.setSourceDataSizeStr((r.format(allDataSizes/(1024*1024))));
					  newVo.setGrabStartDate(CrawlStatisticsVOFilter.minDate(old.getGrabdate(), newVo.getGrabdate()));
					  newVo.setGrabEndDate(CrawlStatisticsVOFilter.maxDate(old.getGrabdate(), newVo.getGrabdate()));
					  
		              map.put(key,newVo);
		       }else{
		          map.put(key,old);
		       }
			}
		}
		Object s[] = map.keySet().toArray();
		for(int i = 0; i < map.size(); i++) {
			StatisticsTotalEntityVO stu=map.get(s[i]);
		}
		List voList = new ArrayList();
		Iterator iter = map.entrySet().iterator();  //获得map的Iterator
		while(iter.hasNext()) {
			Entry entry = (Entry)iter.next();
			voList.add(entry.getValue());
		}
		return voList;
	}
	

	
	
	
	
	@Override
	public List<StatisticsTotalEntityVO> getCrawlTotalRecordByLines(
			String grabdate, String grabchannelID) throws Exception {
		StatisticsTotalEntity tqe = new StatisticsTotalEntity();
		List<StatisticsTotalEntityVO> listVO=new ArrayList<StatisticsTotalEntityVO>();
		List<StatisticsTotalEntity> list=new ArrayList<StatisticsTotalEntity>();
		list=tqe.getTotalRecordByDateAndChannel(grabdate, grabchannelID);
		for (StatisticsTotalEntity entity : list) {
			StatisticsTotalEntityVO vo=new StatisticsTotalEntityVO();
			vo.setFromCityName(entity.getFromCityName());
			vo.setGrabchannel(entity.getGrabchannel());
			vo.setGrabChannelID(entity.getGrabchannelID());
			vo.setActualNum(entity.getActualNum());
			vo.setAllbyteLength(entity.getAllbyteLength());
			vo.setSuccessNum(entity.getSuccessNum());
			vo.setToCityName(entity.getToCityName());
			vo.setStatus(entity.getStatus());
			vo.setGrabdate(entity.getGrabdate());
			vo.setFromCity(entity.getFromCity());
			vo.setToCity(entity.getToCity());
			vo.setFailNum(entity.getFailNum());
			vo.setStartTime(entity.getStartTime());
//			vo.setTotal(vo.getFailNum()+vo.getSuccessNum());
			vo.setTotal(entity.getTotal());
			vo.setLeg(entity.getFromCityName()+"("+entity.getFromCity()+")--"+
					entity.getToCityName()+"("+entity.getToCity()+")");
//			vo.setWasteTime(countWasteTime(entity.getEndTime(),entity.getStartTime()));
			vo.setMaxTime(entity.getMaxTime());
			vo.setMinTime(entity.getMinTime());
			vo.setSourceDataSize(entity.getSourceDataSize());
			vo.setAvgTime(avgTime(entity.getMaxTime(),entity.getMinTime()));
			
			if(entity.getStatus()==-2l){ 			// -2L表示      抓取失败
				vo.setSecondFail(entity.getFailNum());
			}else if(entity.getStatus()==-3l){ 	//-3表示    解析失败  
				vo.setThirdFail(entity.getFailNum());
			}else if(entity.getStatus()==-4l){		//-4表示     其他失败
				vo.setForthFail(entity.getFailNum());
			}else if(entity.getStatus()==-10l){	//-10表示  示定义失败
				vo.setOtherFail(entity.getFailNum());
			}else if(entity.getStatus()==0l){      //  0 表示 等待
				vo.setWaiting(entity.getFailNum());
			}else if(entity.getStatus()== 1l){      //　1　启动中
				vo.setStarting(entity.getFailNum());
			}else if(entity.getStatus()==2l){      // 2  执行中
				vo.setWorking(entity.getFailNum());
			}
			
			listVO.add(vo);
		}
//		return listVO;
		return pingjieMethod(listVO);
	}
	
	private List pingjie2(List<StatisticsTotalDetailEntityVO> list,String startTimeStr,String endTimeStr){
		Map<String,StatisticsTotalDetailEntityVO> map=new LinkedHashMap<String,StatisticsTotalDetailEntityVO>();
		for (int i = 0; i < list.size(); i++) {
			StatisticsTotalDetailEntityVO old=list.get(i);
			//String key=old.getGrabdate()+"--"+old.getFromCity()+"--"+old.getFlightDate()+"--"+old.getToCity();//拼装成一个key
			Date startDateTime=old.getStartTime();
			Date endDateTime=old.getEndTime();
			Long startDateTime_to=0L;
			Long endDateTime_to =0L;
			Long toStartDateTime =0L;
			Long toEndDateTime=0L;
			if(endDateTime !=null && startDateTime!=null){
				startDateTime_to =DateUtils.strToDate(DateUtils.dateToStr(startDateTime, "yyyy-MM-dd HH:mm:ss"),"yyyy-MM-dd HH:mm:ss").getTime();
				endDateTime_to =DateUtils.strToDate(DateUtils.dateToStr(endDateTime, "yyyy-MM-dd HH:mm:ss"),"yyyy-MM-dd HH:mm:ss").getTime();
				//传过来的过滤时间
				toStartDateTime=DateUtils.strToDate(startTimeStr, "yyyy-MM-dd HH:mm:ss").getTime();
				toEndDateTime=DateUtils.strToDate(endTimeStr, "yyyy-MM-dd HH:mm:ss").getTime();
			}
			String key=old.getFromCity()+"--"+old.getFlightDate()+"--"+old.getToCity();//拼装成一个key
			if(flag(old.getFlightDate()) ){
				if(map.get( key )!=null && startDateTime_to>0 &&(startDateTime_to<=toStartDateTime && endDateTime_to<=toEndDateTime)){//判断已经存在同个日期，同个渠道，成功数相同，实际抓取数相同，同一出发城市，同一到到达城市 的数据
					int k=1;
					k++;
					StatisticsTotalDetailEntityVO newVo=map.get(key);//将存在的数据取出来
					  //累加
					  Long total=nullToLong(old.getTotal(),newVo.getTotal());
					  
					  Long failNum=nullToLong(old.getFailNum(),newVo.getFailNum());
					  Long secondFail=nullToLong(old.getSecondFail(),newVo.getSecondFail());
					  Long thirdFail=nullToLong(old.getThirdFail(),newVo.getThirdFail());
					  Long forthFail=nullToLong(old.getForthFail(),newVo.getForthFail());
					  Long otherFail=nullToLong(old.getOtherFail(),newVo.getOtherFail());
					  Long waiting=nullToLong(old.getWaiting(),newVo.getWaiting());
					  Long starting=nullToLong(old.getStarting(),newVo.getStarting());
					  Long working=nullToLong(old.getWorking(),newVo.getWorking());
					  Long actualNum=nullToLong(old.getActualNum(), newVo.getActualNum());
					  Long successNum=nullToLong(old.getSuccessNum(), newVo.getSuccessNum());
					  Long maxTime=nullToLong(old.getMaxTime(), newVo.getMaxTime());
					  Long minTime=nullToLong(old.getMinTime(), newVo.getMinTime());
					  newVo.setTotal(total);//total在数据库中多加了一遍成功数
					  newVo.setFailNum(failNum);
					  newVo.setSecondFail(secondFail); 
					  newVo.setThirdFail(thirdFail);
					  newVo.setForthFail(forthFail);
					  newVo.setOtherFail(otherFail);
					  newVo.setWaiting(waiting);
					  newVo.setWorking(working);
					  newVo.setStarting(starting);
					  newVo.setMaxTime(maxTime/k);
					  newVo.setMinTime(minTime/k);
					  newVo.setActualNum(actualNum/k);
					  newVo.setSuccessNum(successNum);
					  
					  
		              map.put(key,newVo);
		       }else{
		          map.put(key,old);
		       }
			}
		}
		Object s[] = map.keySet().toArray();
		for(int i = 0; i < map.size(); i++) {
			StatisticsTotalDetailEntityVO stu=map.get(s[i]);
		}
		List voList = new ArrayList();
		Iterator iter = map.entrySet().iterator();  //获得map的Iterator
		while(iter.hasNext()) {
			Entry entry = (Entry)iter.next();
			voList.add(entry.getValue());
		}
		return voList;
	}
	
	
	//与当前日期比较大小 2014-03-05
	public boolean flag(String dateStr){
		boolean flag=false;
		String nowaDaysStr=DateUtils.getDateStr("yyyy-MM-dd");
		Long nowaDays=DateUtils.strToDate(nowaDaysStr, "yyyy-MM-dd").getTime();
		Long dateDate=DateUtils.strToDate(dateStr, "yyyy-MM-dd").getTime();
		if(nowaDays<=dateDate){
			flag=true;
		}
		
		return flag;
	}
	
	//处理String XX@XX,XX@XX
	public static Map<String,String> doStrings(String s){
		Map<String,String> endStr=new HashMap<String ,String>();
		String s1[]=s.split(",");
		int sLength=s1.length;
		for(int i=0;i<sLength;i++){
			String hbaseKey=s1[i].split("@")[0];
			String datas=s1[i].split("@")[1];
			endStr.put(hbaseKey, datas);
		}
		
		return endStr;
	}

	//处理String XX,XX,XX
	public static Map<String,Integer> doStrings2(String s){
		Map<String,Integer> endStr=new HashMap<String ,Integer>();
		String s2[]=s.split(",");
		int sLength=s2.length;
		for(int i=0;i<sLength;i++){
			String hbaseKey=s2[i];
			endStr.put(hbaseKey, i+1);
		}
		
		return endStr;
	}
	
	//日期格式：yyyy-MM-dd 排序处理
	public class sortClass implements Comparator{
		@Override
		public int compare(Object o1, Object o2) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			CrawlStatisticsVO object1=(CrawlStatisticsVO)o1;
			CrawlStatisticsVO object2=(CrawlStatisticsVO)o2;
			Date d1=null;
			Date d2=null;
			if(object1.getDateStr()!=null ){
				try{
					d1=sdf.parse(object1.getDateStr());
					d2=sdf.parse(object2.getDateStr());
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
			if(d1 ==null && d2==null)
				return 0;
			if(d1 ==null )
				return 1;
			if(d2==null)
				return -1;
			return d2.compareTo(d1);

		}
		
	}
	
}
